package cn.pzhu.edu.util;
	import java.io.IOException;
	import java.io.InputStream;
	import java.sql.Connection;
	import java.sql.DriverManager;
	import java.sql.PreparedStatement;
	import java.sql.ResultSet;
	import java.sql.SQLException;
	import java.sql.Statement;
	import java.util.Properties;

	public class JdbcUtil {
	  private static String driver =null;
	  private static String url= null;
	  private static String user= null;
	  private static String password= null;	
	  static{
		  InputStream inputStream = JdbcUtil.class.getClassLoader().getResourceAsStream("db.properties");
			Properties properties = new Properties();
			try {
				properties.load(inputStream);
				driver = properties.getProperty("driver");
				url = properties.getProperty("url");
				user = properties.getProperty("user");
				password = properties.getProperty("password");			
				try {
					Class.forName(driver);
				} catch (ClassNotFoundException e) {
					e.printStackTrace();
				}	
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	  public static Connection getConnection(){
			try {
				return DriverManager.getConnection(url, user, password);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return null;
			}
		}
	  public static void close(Connection con,Statement sta,ResultSet res){
		  if (res!=null) {
			  try {
				res.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		  if (sta!=null) {
			  try {
				sta.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		  if (con!=null) {
			  try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	  }
	  public static void close(ResultSet res,Statement sta,Connection con){
		  close(null, sta, res);
	  }
	  public static void close(Connection con,PreparedStatement sta,ResultSet res){
			close(con, (Statement)sta, res);
		}
	  public static void close(Connection con,PreparedStatement sta){
			close(con, (Statement)sta, null);
		}
	}

